package com.sl.au.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.BusinesssellerBaseInfo;
import com.sl.au.entity.KitchenWaste;
import com.sl.au.entity.Module;
import com.sl.au.service.BusinesssellerBaseInfoService;
import com.sl.au.service.KitchenWasteService;
import com.sl.utils.MsgHelper;

import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "/ui/kitchenwaste")
public class KitchenWasteController extends BaseController {

	@Autowired
	private KitchenWasteService kitchenWasteService;

	@Autowired
	private BusinesssellerBaseInfoService businesssellerBaseInfoService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView, HttpServletRequest request) {
		String device = request.getParameter("device");
		String url = request.getParameter("url");
		String identify = request.getParameter("identify");
		String userId = this.getMyAccount().getUser().getId();
		// 移动端
		if (StringUtils.hasText(device) && this.DEVICE.equals(device)) {
			// 移动端访问的首页
			modelAndView.setViewName("mobile/kitchenwaste/indexOnPhone");
			modelAndView.addObject("bussinessId", identify);
			modelAndView.addObject("device", device);
			modelAndView.addObject("url", url);

		} else {
			// PC端
			modelAndView = createLayoutView("kitchenWaste/kitchenWasteindex");
		}
		return modelAndView;
	}

	@RequestMapping(value = "saveKitchenWaste", method = RequestMethod.GET)
	public ModelAndView saveKitchenWaste(ModelAndView modelAndView, HttpServletRequest request) {
		String device = request.getParameter("device");
		String url = request.getParameter("url");
		String identify = request.getParameter("identify");
		String userId = this.getMyAccount().getUser().getId();
		// 移动端
		if (StringUtils.hasText(device) && this.DEVICE.equals(device)) {
			// 移动端访问的首页
			modelAndView.setViewName("mobile/kitchenwaste/saveKitchenWaste");
			modelAndView.addObject("bussinessId", identify);
			modelAndView.addObject("device", device);
			modelAndView.addObject("url", url);

		} else {
			// PC端
		}
		return modelAndView;
	}

	@RequestMapping("/pageOnPhone")
	public MsgHelper getPageInfoOnPhone() {
		String device = request.getParameter("device");
		String identify = request.getParameter("identify");// 这个商户的id
		if (!this.DEVICE.equals(device) && !StringUtils.hasText(device)) {
		}
		// 初始化页面
		List<KitchenWaste> kitchenWasteHistoryInfo = kitchenWasteService.findKitchenWasteByHistory(0, 10, identify,
				null, null);

		return MsgHelper.success().put("kitchenWasteHistoryInfo", kitchenWasteHistoryInfo);
	}

	@RequestMapping("/pageOnPhoneToDay")
	public MsgHelper getPageInfoOnPhoneToDay() {
		String device = request.getParameter("device");
		String identify = request.getParameter("identify");// 这个商户的id
		if (!this.DEVICE.equals(device) && !StringUtils.hasText(device)) {
		}
		// 初始化页面
		List<KitchenWaste> kitchenWasteToDayInfo = kitchenWasteService.findKitchenWasteByToDay(0, 10, identify, null,
				null);

		return MsgHelper.success().put("kitchenWasteToDayInfo", kitchenWasteToDayInfo);
	}

	@RequestMapping(value = "/saveKitchenWasteWithFile", method = RequestMethod.POST)
	public MsgHelper saveCheckAndDetailWithFile(MultipartHttpServletRequest requests) {
		// 获取上传图片信息
		MultipartFile file = requests.getFile("file");
		// 获取当前Check图片的存储路径
		String url = "";
		if (file != null && !file.isEmpty()) {
			// 注意路径的构建
			url = getUploadFilePath(this.fileUploadPath, "img/kitchenWaste", file);
		}
		// 获取商户信息
		BusinesssellerBaseInfo info = businesssellerBaseInfoService
				.findByBussinessSellerId(request.getParameter("bussinessId"));
		// 获取商户废弃物填报信息
		JSONObject project = JSONObject.fromObject(request.getParameter("kitchenWaste"));
		KitchenWaste kitchenWaste = (KitchenWaste) JSONObject.toBean(project, KitchenWaste.class);
		kitchenWaste.setBusinesssellerBaseInfo(info);
		kitchenWaste.setCheckPerson(getMyAccount().getUser());
		kitchenWaste.setPicAddr(url);
		kitchenWasteService.getRepository().save(kitchenWaste);
		MsgHelper msgHelper = MsgHelper.success();
		return msgHelper;
	}

	@RequestMapping("/findById/{id}")
	public MsgHelper findById(@PathVariable("id") String id) {
		KitchenWaste kitchenWaste = kitchenWasteService.getRepository().findOne(id);
		return MsgHelper.success().put("kitchenWaste", kitchenWaste);
	}

	@RequestMapping(value = "/updateKitchenWasteWithFile", method = RequestMethod.POST)
	public MsgHelper updateCheckAndDetailWithFile(MultipartHttpServletRequest requests) {
		// 获取上传图片信息
		MultipartFile file = requests.getFile("file");
		// 获取当前Check图片的存储路径

		// 获取原来厨房废弃物信息
		KitchenWaste oldKitchenWaste = kitchenWasteService.getRepository()
				.findOne(request.getParameter("kitchenWasteId"));
		String url = oldKitchenWaste.getPicAddr();
		if (file != null && !file.isEmpty()) {
			// 注意路径的构建
			url = getUploadFilePath(this.fileUploadPath, "img/kitchenWaste", file);
		}

		// 获取商户废弃物填报信息
		JSONObject project = JSONObject.fromObject(request.getParameter("kitchenWaste"));
		KitchenWaste kitchenWaste = (KitchenWaste) JSONObject.toBean(project, KitchenWaste.class);
		oldKitchenWaste.setUpdateTime(kitchenWaste.getUpdateTime());
		oldKitchenWaste.setWaste(kitchenWaste.getWaste());
		oldKitchenWaste.setWasteOil(kitchenWaste.getWasteOil());
		oldKitchenWaste.setRemark(kitchenWaste.getRemark());
		oldKitchenWaste.setCheckPerson(getMyAccount().getUser());
		oldKitchenWaste.setPicAddr(url);

		kitchenWasteService.getRepository().save(oldKitchenWaste);
		MsgHelper msgHelper = MsgHelper.success();
		return msgHelper;
	}

	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		Account account = getMyAccount();
		String roleName = account.getRole().getName();
		String userId = account.getUser().getId();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key = request.getParameter("key");
		List<KitchenWaste> kitchenWastes = null;
		int count = 0;
		// Module pageModule = getPageModule("kitchenWasteLooking");
		if ("系统管理员".equals(roleName)) {
			if (StringUtils.hasText(key)) {
				// 管理员带查询条件
				kitchenWastes = kitchenWasteService.getAdminPageByKey(start, length, key);
			} else {
				// 管理员不带查询条件
				kitchenWastes = kitchenWasteService.getAdminPageWithoutKey(start, length);
			}
			count = kitchenWasteService.getAdminCount(key);
		} else {
			if (StringUtils.hasText(key)) {
				// 其他带查询条件
				kitchenWastes = kitchenWasteService.getOtherPageByKey(start, length, key, userId);
			} else {
				kitchenWastes = kitchenWasteService.getOtherPageWithoutKey(start, length, userId);
			}
			count = kitchenWasteService.getOtherCount(userId, key);
		}

		map.put("data", kitchenWastes);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
}
